<?php
/**
 * File: Request.php.
 * User: Yan<me@xiaoyan.me>
 * DateTime: 2017-12-19 20:54
 */

namespace App\Core;

use App\Models\UserInfo;

class Request
{
    /**
     * @var RequestParam
     */
    public $query;
    /**
     * @var RequestParam
     */
    public $post;
    /**
     * @var RequestParam
     */
    public $session;

    /**
     * @var UserInfo
     */
    public $user = null;
    public $openid;

    public function __construct($query = [], $post = [])
    {
        $this->query = new RequestParam($query);
        $this->post = new RequestParam($post);
        $this->session = new RequestParam($_SESSION);
        $this->is_wechat = preg_match('/micromessenger/',strtolower($_SERVER['HTTP_USER_AGENT']));
    }

    public static function GetBaseRequest()
    {
        return new Request($_GET, $_POST);
    }
    

    public function isAjax()
    {
        if ($this->query->get('_request') == 'ajax'
            || $this->query->get('request.method') == 'ajax'
        ) {
            return true;
        }
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
            && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
        ) {
            return true;
        }
        return false;
    }

    /**
     * @param $key
     * @return int
     */
    public function getInt($key)
    {
        return $this->query->getInt($key);
    }

    public function get($key, $default = null)
    {
        return $this->query->get($key, $default);
    }

    public function posts(){
        return $this->post->getAll();
    }
    public function post($key, $default = null)
    {
        return $this->post->get($key, $default);
    }
    public function session($key, $default = null)
    {
        return $this->session->get($key, $default);
    }

    /**
     * @param UserInfo $user
     */
    public function setUser($user)
    {
        $this->user = $user;
    }

    public function getPath()
    {
        return getRequestUri();
    }
    private $is_wechat = false;
    public function is_weixin(){
        return $this->is_wechat;
    }
}